package com.spider.admin.service.agent;


import com.spider.admin.exception.SpiderException;
import com.spider.admin.http.entity.PageResult;
import com.spider.admin.store.primary.mapper.AgentSettingMapper;
import com.spider.admin.store.primary.model.AgentSettingInfo;
import com.spider.admin.utils.CommonUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AgentSettingServiceImpl implements AgentSettingService {

    @Autowired
    private AgentSettingMapper agentSettingMapper;

    @Override
    public PageResult<AgentSettingInfo> queryAgentSettingList(Integer page, Integer pageSize, String section, String optionItem, String belongType, String belongId) throws SpiderException {
        page = page == null || page.intValue() <= 0 ? 1 : page;
        pageSize = pageSize == null ? 10 : pageSize;
        if (StringUtils.isNotEmpty(optionItem)) {
            optionItem = "%" + optionItem + "%";
        }
        long totalCount = agentSettingMapper.queryAgentSettingNum(section, optionItem, belongType, belongId).longValue();
        int totalPage = (int) Math.ceil((double) totalCount / (double) pageSize);
        page = page > totalPage ? totalPage : page;
        page = page <= 0 ? 1 : page;
        PageResult<AgentSettingInfo> result = new PageResult<>();
        result.setCurrentPage(page);
        result.setPageSize(pageSize);
        result.setTotal(totalCount);
        result.setTotalPage(totalPage);

        List<AgentSettingInfo> records = agentSettingMapper.queryAgentSettings((page - 1) * pageSize, pageSize, section, optionItem, belongType, belongId);
        result.setList(records);
        return result;
    }

    @Override
    public int saveSetting(AgentSettingInfo agentSettingInfo) throws SpiderException {
        int row = 0;
        if (StringUtils.isEmpty(agentSettingInfo.getPid())) {
            agentSettingInfo.setPid(CommonUtil.getUniqueKey());
            row = agentSettingMapper.insertSettingInfo(agentSettingInfo);
        } else {
            row = agentSettingMapper.updateSettingInfo(agentSettingInfo);
        }
        return row;
    }

    @Override
    public List<Map<String, String>> queryAgentServerList(String tenantId, Integer roleType) throws SpiderException {
        List<Map<String, String>> result = agentSettingMapper.queryAgentServerList(tenantId);
        if (roleType != null && roleType.intValue() == 1) {
            if (result == null) {
                result = new ArrayList<>();
            }
            Map<String, String> globalInfoMap = new HashMap<>();
            globalInfoMap.put("pid", "global");
            globalInfoMap.put("nodeId", "global");
            globalInfoMap.put("nodeName", "全局配置");
            globalInfoMap.put("nodeType", "global");
            globalInfoMap.put("parentId", "");
            result.add(globalInfoMap);
        }
        return result;
    }

    @Override
    public int deleteWithOptionPid(String pid) throws SpiderException {
        return agentSettingMapper.deleteWithOptionPid(pid);
    }

    @Override
    public List<Map<String, String>> queryAgentServerList2(String tenantId, Integer roleType) throws SpiderException {
        List<Map<String, String>> result = agentSettingMapper.queryAgentServerList2(tenantId);
        if (roleType != null && roleType.intValue() == 1) {
            if (result == null) {
                result = new ArrayList<>();
            }
            Map<String, String> globalInfoMap = new HashMap<>();
            globalInfoMap.put("pid", "global");
            globalInfoMap.put("nodeId", "global");
            globalInfoMap.put("nodeName", "全局配置");
            globalInfoMap.put("nodeType", "global");
            globalInfoMap.put("parentId", "");
            result.add(globalInfoMap);
        }
        return result;
    }
}
